package com.example.amap.tencent.lifecycle

import android.content.Context
import android.os.Build
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.amap.lib.base.utils.LogUtil
import com.example.amap.map.IMapView
import com.example.amap.service.map.IMapService
import com.example.amap.tencent.map.TencentMapView
import com.tencent.navix.api.NavigatorConfig
import com.tencent.navix.api.NavigatorZygote
import com.tencent.tencentmap.mapsdk.maps.MapView
import com.tencent.tencentmap.mapsdk.maps.TencentMapInitializer
import java.lang.ref.WeakReference

@Route(path = "/map/tencent/MapServiceImpl")
class MapServiceImpl : IMapService {
    companion object {
        private const val TAG = "LifecycleService"
    }

    private var mapViewWeakReference: WeakReference<MapView?>? = null

    override fun createMapView(context: Context): IMapView {
        super.createMapView(context)
        mapViewWeakReference = WeakReference(MapView(context))
        return TencentMapView(mapViewWeakReference?.get())
    }

    override fun sdkInit(context: Context?) {
        context?.let {
            TencentMapInitializer.setAgreePrivacy(it, true)
            TencentMapInitializer.start(it)
            NavigatorZygote.with(it).init(
                NavigatorConfig.builder()
                    .setUserAgreedPrivacy(true)         // 设置同意隐私协议
                    .setDeviceId("custom_device_id")    // 设置自定义的设备ID，用于问题排查
                    .setDeviceModel(Build.MODEL)        // 设置设备型号，帮助问题排查
                    .build())
            NavigatorZygote.with(it).start()
        }
    }

    override fun onStart() {
        super.onStart()
        LogUtil.d(TAG, "onStart")
        mapViewWeakReference?.get()?.onStart()
    }

    override fun onResume() {
        super.onResume()
        LogUtil.d(TAG, "onResume")
        mapViewWeakReference?.get()?.onResume()
    }

    override fun onPause() {
        super.onPause()
        LogUtil.d(TAG, "onPause")
        mapViewWeakReference?.get()?.onPause()
    }

    override fun onStop() {
        super.onStop()
        LogUtil.d(TAG, "onStop")
        mapViewWeakReference?.get()?.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtil.d(TAG, "onDestroy")
        mapViewWeakReference?.get()?.onDestroy()
    }
}